package com.hjwoo.app.views;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.ui.TimeDialog;
import com.hjwoo.app.utils.MyTimeUtils;
import com.hjwoo.app.utils.Utils;

import java.util.Date;

/**
 * Created by renxiaowen on 2016/8/2
 * 预约下户时间
 */
public class AppointmentTimeActivity extends BaseAppCompatActivity implements View.OnClickListener {
    //other界面上的标记
    private static final int OTHER_TIME = 0;
    private static final int DATE_TIME = 1;
    //将每一个button设置一个返回码的标记
    private static final int BTNTIME_TXT1 = 2;
    private static final int BTNTIME_TXT2 = 3;
    private static final int BTNTIME_TXT3 = 4;
    private static final int BTNTIME_TXT4 = 5;
    private static final int BTNTIME_TXT5 = 6;
    private static final int BTNTIME_TXT6 = 7;
    private static final int BTNTIME_TXT7 = 8;
    private static final int BTNTIME_TXT8 = 9;
    private static final int BTNTIME_TXT9 = 10;
    private static final int BTNTIME_TXT10 = 11;
    private static final int BTNTIME_TXT11 = 12;
    private static final int BTNTIME_TXT12 = 13;
    private static final int BTNTIME_TXT13 = 14;
    private static final int BTNTIME_TXT14 = 15;
    private static final int BTNTIME_TXT15 = 16;
    private static final int BTNTIME_TXT16 = 17;
    private static final int BTNTIME_TXT17 = 18;
    private Button btnBack, btnOk1, btnOk2, btnOk3, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14, btn15, btn16;
    private TextView evaluate_child_labes_01, evaluate_child_labes_02, evaluate_child_labes_03, other_date, other_time;
    private TextView evaluate_child_labes_line_01, evaluate_child_labes_line_02, evaluate_child_labes_line_03;
    private RelativeLayout time1, time2, other;
    private LinearLayout ll_today, ll_tomorrow, ll_other, ib_appointment_time, ib_appointment_date;
    private Intent intent;
    private Drawable drawable;
    private int flag = 0;
    private String tomorrowStringDate;//明天日期
    private String curStringDate1;//今天日期8月8日
    private String strYear;//当前年份
    private String currDay;//08-08  格式
    private String time;//上一个页面传来的时间
    private String tvDate;//other界面上的日期
    private String tvTime;//Other界面上的时间
    private String MD;//08月14日  格式
    private String T;//13:00 - 14:00
    private String strData;//返回到other界面上的日期
    private String strTime;//返回到other界面上的时间
    private TimeDialog timeDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appointment_time);
        Utils.initTitle("预约时间", this, true);
        initView();
        initOnClickListener();
        intent = getIntent();
        btnHideAndVisibile();
        setDate();
        setOrangeBackground();//如果用户选过时间,需要在用户选择的时间上加背景框
    }


    @Override
    protected void onStart() {
        super.onStart();
        initView();
        btnHideAndVisibile();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
        btnHideAndVisibile();
    }
    private void initView() {
        btnBack = (Button) findViewById(R.id.btnBack);
        btnOk1 = (Button) findViewById(R.id.btnOk1);
        btnOk2 = (Button) findViewById(R.id.btnOk2);
        btnOk3 = (Button) findViewById(R.id.btnOk3);
        //三个选项
        evaluate_child_labes_01 = (TextView) findViewById(R.id.evaluate_child_labes_01);
        evaluate_child_labes_02 = (TextView) findViewById(R.id.evaluate_child_labes_02);
        evaluate_child_labes_03 = (TextView) findViewById(R.id.evaluate_child_labes_03);
        //三个布局
        ll_today = (LinearLayout) findViewById(R.id.ll_today);
        ll_tomorrow = (LinearLayout) findViewById(R.id.ll_tomorrow);
        ll_other = (LinearLayout) findViewById(R.id.ll_other);
        //三条线
        evaluate_child_labes_line_01 = (TextView) findViewById(R.id.evaluate_child_labes_line_01);
        evaluate_child_labes_line_02 = (TextView) findViewById(R.id.evaluate_child_labes_line_02);
        evaluate_child_labes_line_03 = (TextView) findViewById(R.id.evaluate_child_labes_line_03);

        //三个布局
        time1 = (RelativeLayout) findViewById(R.id.time1);
        time2 = (RelativeLayout) findViewById(R.id.time2);
        other = (RelativeLayout) findViewById(R.id.other);
        //时间按钮
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        btn5 = (Button) findViewById(R.id.btn5);
        btn6 = (Button) findViewById(R.id.btn6);
        btn7 = (Button) findViewById(R.id.btn7);
        btn8 = (Button) findViewById(R.id.btn8);
        btn9 = (Button) findViewById(R.id.btn9);
        btn10 = (Button) findViewById(R.id.btn10);
        btn11 = (Button) findViewById(R.id.btn11);
        btn12 = (Button) findViewById(R.id.btn12);
        btn13 = (Button) findViewById(R.id.btn13);
        btn14 = (Button) findViewById(R.id.btn14);
        btn15 = (Button) findViewById(R.id.btn15);
        btn16 = (Button) findViewById(R.id.btn16);

        //点击其他页面，两个按钮
        ib_appointment_date = (LinearLayout) findViewById(R.id.ib_appointment_date);
        ib_appointment_time = (LinearLayout) findViewById(R.id.ib_appointment_time);
        //other界面上的textView
        other_date = (TextView) findViewById(R.id.other_date);
        other_time = (TextView) findViewById(R.id.other_time);
        //选择时间
        timeDialog = new TimeDialog(
                AppointmentTimeActivity.this, R.style.Theme_Popdialog, "请选择预约时间");
    }


    private void initOnClickListener() {
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn10.setOnClickListener(this);
        btn11.setOnClickListener(this);
        btn12.setOnClickListener(this);
        btn13.setOnClickListener(this);
        btn14.setOnClickListener(this);
        btn15.setOnClickListener(this);
        btn16.setOnClickListener(this);
        //三个布局注册监听
        ll_today.setOnClickListener(this);
        ll_tomorrow.setOnClickListener(this);
        ll_other.setOnClickListener(this);
        //返回
        btnBack.setOnClickListener(this);
        //确认返回时间
        btnOk1.setOnClickListener(this);
        btnOk2.setOnClickListener(this);
        btnOk3.setOnClickListener(this);
        ib_appointment_date.setOnClickListener(this);
        ib_appointment_time.setOnClickListener(this);
    }

    //初始化第一天的按钮
    void initbtn() {
        drawable = getResources().getDrawable(R.drawable.btn_gray_mini);
        btn1.setBackground(drawable);
        btn2.setBackground(drawable);
        btn3.setBackground(drawable);
        btn4.setBackground(drawable);
        btn5.setBackground(drawable);
        btn6.setBackground(drawable);
        btn7.setBackground(drawable);
        btn8.setBackground(drawable);
    }
    //初始化第二天的按钮
    void initTomorrowBtn() {
        drawable = getResources().getDrawable(R.drawable.btn_gray);
        btn9.setBackground(drawable);
        btn10.setBackground(drawable);
        btn11.setBackground(drawable);
        btn12.setBackground(drawable);
        btn13.setBackground(drawable);
        btn14.setBackground(drawable);
        btn15.setBackground(drawable);
        btn16.setBackground(drawable);
    }

    private void setDate() {
        //设置当前日期
        curStringDate1 = MyTimeUtils.getSystemDate();//08月08日
        evaluate_child_labes_01.setText(curStringDate1);
        //将当天时间转成date格式
        Date curDate2 = MyTimeUtils.str2Date(curStringDate1);
        //获取当天日期
        currDay = MyTimeUtils.getMonthAndDay();
        //设置明天时间
        Date tomorrowDate = MyTimeUtils.addDays(curDate2, 1);
        tomorrowStringDate = MyTimeUtils.getTomDate();
        //格式化明天日期
        evaluate_child_labes_02.setText(MyTimeUtils.Date2String(tomorrowDate));
        //获取年份
        strYear = MyTimeUtils.getSystemYear();
    }

    //设置背景
    private void setOrangeBackground() {
        //时间格式 2016-08-08 14:00 - 15:00
        time = intent.getStringExtra("strTime");
        if (!TextUtils.isEmpty(time)) {
            String[] Strs = time.split(" ");
            String yearAndMonth = Strs[0];//2016-08-08
            String[] monthAndDay = yearAndMonth.split("-");
            String month = monthAndDay[1];//08
            String day = monthAndDay[2];//08
            //08月09日
            MD = month + "月" + day + "日";
            String startTime = Strs[1];//14:00
            String endTime = Strs[3];//15:00
            //14:00 - 15:00
            T = startTime + " - " + endTime;
            //根据日期判断那个条目显示
            drawable = getResources().getDrawable(R.drawable.btn_time_orange);
            if (evaluate_child_labes_01.getText().toString().equals(MD)) {
                showToday();
                if (btn1.getText().toString().equals(T)) {
                    btn1.setBackground(drawable);
                    flag = 1;
                } else if (btn2.getText().toString().equals(T)) {
                    btn2.setBackground(drawable);
                    flag = 2;
                } else if (btn3.getText().toString().equals(T)) {
                    btn3.setBackground(drawable);
                    flag = 3;
                } else if (btn4.getText().toString().equals(T)) {
                    btn4.setBackground(drawable);
                    flag = 4;
                } else if (btn5.getText().toString().equals(T)) {
                    btn5.setBackground(drawable);
                    flag = 5;
                } else if (btn6.getText().toString().equals(T)) {
                    btn6.setBackground(drawable);
                    flag = 6;
                } else if (btn7.getText().toString().equals(T)) {
                    btn7.setBackground(drawable);
                    flag = 7;
                } else if (btn8.getText().toString().equals(T)) {
                    btn8.setBackground(drawable);
                    flag = 8;
                }
            } else if (evaluate_child_labes_02.getText().toString().equals(MD)) {
                showTomorrow();
                if (btn9.getText().toString().equals(T)) {
                    btn9.setBackground(drawable);
                    flag = 9;
                } else if (btn10.getText().toString().equals(T)) {
                    btn10.setBackground(drawable);
                    flag = 10;
                } else if (btn11.getText().toString().equals(T)) {
                    btn11.setBackground(drawable);
                    flag = 11;
                } else if (btn12.getText().toString().equals(T)) {
                    btn12.setBackground(drawable);
                    flag = 12;
                } else if (btn13.getText().toString().equals(T)) {
                    btn13.setBackground(drawable);
                    flag = 13;
                } else if (btn14.getText().toString().equals(T)) {
                    btn14.setBackground(drawable);
                    flag = 14;
                } else if (btn15.getText().toString().equals(T)) {
                    btn15.setBackground(drawable);
                    flag = 15;
                } else if (btn16.getText().toString().equals(T)) {
                    btn16.setBackground(drawable);
                    flag = 16;
                }
            } else {
                showOther();
                if (!TextUtils.isEmpty(MD) && !TextUtils.isEmpty(T)) {//从上一个页面过来的
                    other_date.setText(MD);
                    other_time.setText(T);
                }

            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnBack: //返回上一页
                finish();
                break;
            case R.id.ll_today:
                btnHideAndVisibile();//控制Button的显示与隐藏
                showToday();
                break;
            case R.id.ll_tomorrow:
                initTomorrowBtn();
                showTomorrow();
                break;
            case R.id.ll_other:
                showOther();
                //判断是否设置日期和时间
                if (!TextUtils.isEmpty(MD) && !TextUtils.isEmpty(T)) {//从上一个页面过来的
                    if (MD.equals(evaluate_child_labes_01.getText().toString()) || MD.equals(evaluate_child_labes_02.getText().toString())){
                    other_date.setText("");
                    other_time.setText("");
                } else{
                    other_date.setText(MD);
                    other_time.setText(T);
                  }
                }
                break;
            case R.id.btn1:
                flag = 1;
                initbtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn1.setBackground(drawable);
                break;
            case R.id.btn2:
                flag = 2;
                initbtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn2.setBackground(drawable);
                break;
            case R.id.btn3:
                flag = 3;
                initbtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn3.setBackground(drawable);
                break;
            case R.id.btn4:
                flag = 4;
                initbtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn4.setBackground(drawable);
                break;
            case R.id.btn5:
                flag = 5;
                initbtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn5.setBackground(drawable);
                break;
            case R.id.btn6:
                flag = 6;
                initbtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn6.setBackground(drawable);
                break;
            case R.id.btn7:
                flag = 7;
                initbtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn7.setBackground(drawable);
                break;
            case R.id.btn8:
                flag = 8;
                initbtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn8.setBackground(drawable);
                break;
            case R.id.btn9:
                flag = 9;
                initbtn();
                initTomorrowBtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn9.setBackground(drawable);
                break;
            case R.id.btn10:
                flag = 10;
                initbtn();
                initTomorrowBtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn10.setBackground(drawable);
                break;
            case R.id.btn11:
                flag = 11;
                initbtn();
                initTomorrowBtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn11.setBackground(drawable);
                break;
            case R.id.btn12:
                flag = 12;
                initbtn();
                initTomorrowBtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn12.setBackground(drawable);
                break;
            case R.id.btn13:
                flag = 13;
                initbtn();
                initTomorrowBtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn13.setBackground(drawable);
                break;
            case R.id.btn14:
                flag = 14;
                initbtn();
                initTomorrowBtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn14.setBackground(drawable);
                break;
            case R.id.btn15:
                flag = 15;
                initbtn();
                initTomorrowBtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn15.setBackground(drawable);
                break;
            case R.id.btn16:
                flag = 16;
                initbtn();
                initTomorrowBtn();
                drawable = getResources().getDrawable(R.drawable.btn_time_orange);
                btn16.setBackground(drawable);
                break;
            case R.id.ib_appointment_time://弹出选择时间
                intent = new Intent(AppointmentTimeActivity.this, GetTimeActivity.class);
                startActivityForResult(intent, OTHER_TIME);
                break;
            case R.id.ib_appointment_date://弹出选择日期
                intent = new Intent(AppointmentTimeActivity.this, GetDateActivity.class);
                startActivityForResult(intent, DATE_TIME);
                break;
            case R.id.btnOk1://向预约下户页面传时间
                if (flag == 0) {
                    timeDialog.show();
                } else {
                    switch (flag) {//flag第一个时间页面上的标记
                        case 1://点击btn1,传值
                            String btnTime_txt1 = btn1.getText().toString();
                            btnTime_txt1 = strYear + "-" + currDay + " " + btnTime_txt1;
                            intent.putExtra("btnTime_txt1", btnTime_txt1);
                            this.setResult(BTNTIME_TXT1, intent);
                            break;
                        case 2:
                            String btnTime_txt2 = btn2.getText().toString();
                            btnTime_txt2 = strYear + "-" + currDay + " " + btnTime_txt2;
                            intent.putExtra("btnTime_txt2", btnTime_txt2);
                            this.setResult(BTNTIME_TXT2, intent);
                            break;
                        case 3:
                            String btnTime_txt3 = btn3.getText().toString();
                            btnTime_txt3 = strYear + "-" + currDay + " " + btnTime_txt3;
                            intent.putExtra("btnTime_txt3", btnTime_txt3);
                            this.setResult(BTNTIME_TXT3, intent);
                            break;
                        case 4:
                            String btnTime_txt4 = btn4.getText().toString();
                            btnTime_txt4 = strYear + "-" + currDay + " " + btnTime_txt4;
                            intent.putExtra("btnTime_txt4", btnTime_txt4);
                            this.setResult(BTNTIME_TXT4, intent);
                            break;
                        case 5:
                            String btnTime_txt5 = btn5.getText().toString();
                            btnTime_txt5 = strYear + "-" + currDay + " " + btnTime_txt5;
                            intent.putExtra("btnTime_txt5", btnTime_txt5);
                            this.setResult(BTNTIME_TXT5, intent);
                            break;
                        case 6:
                            String btnTime_txt6 = btn6.getText().toString();
                            btnTime_txt6 = strYear + "-" + currDay + " " + btnTime_txt6;
                            intent.putExtra("btnTime_txt6", btnTime_txt6);
                            this.setResult(BTNTIME_TXT6, intent);
                            break;
                        case 7:
                            String btnTime_txt7 = btn7.getText().toString();
                            btnTime_txt7 = strYear + "-" + currDay + " " + btnTime_txt7;
                            intent.putExtra("btnTime_txt7", btnTime_txt7);
                            this.setResult(BTNTIME_TXT7, intent);
                            break;
                        case 8:
                            String btnTime_txt8 = btn8.getText().toString();
                            btnTime_txt8 = strYear + "-" + currDay + " " + btnTime_txt8;
                            intent.putExtra("btnTime_txt8", btnTime_txt8);
                            this.setResult(BTNTIME_TXT8, intent);
                            break;
                    }
                    finish();
                }
                break;
            case R.id.btnOk2://第二天的时间
                if (flag == 0) {
                    timeDialog.show();
                } else {
                    switch (flag) {//flag第一个时间页面上的标记
                        case 9:
                            String btnTime_txt9 = btn9.getText().toString();
                            btnTime_txt9 = strYear + "-" + tomorrowStringDate + " " + btnTime_txt9;
                            intent.putExtra("btnTime_txt9", btnTime_txt9);
                            this.setResult(BTNTIME_TXT9, intent);
                            break;
                        case 10:
                            String btnTime_txt10 = btn10.getText().toString();
                            btnTime_txt10 = strYear + "-" + tomorrowStringDate + " " + btnTime_txt10;
                            intent.putExtra("btnTime_txt10", btnTime_txt10);
                            this.setResult(BTNTIME_TXT10, intent);
                            break;
                        case 11:
                            String btnTime_txt11 = btn11.getText().toString();
                            btnTime_txt11 = strYear + "-" + tomorrowStringDate + " " + btnTime_txt11;
                            intent.putExtra("btnTime_txt11", btnTime_txt11);
                            this.setResult(BTNTIME_TXT11, intent);
                            break;
                        case 12:
                            String btnTime_txt12 = btn1.getText().toString();
                            btnTime_txt12 = strYear + "-" + tomorrowStringDate + " " + btnTime_txt12;
                            intent.putExtra("btnTime_txt12", btnTime_txt12);
                            this.setResult(BTNTIME_TXT12, intent);
                            break;
                        case 13:
                            String btnTime_txt13 = btn13.getText().toString();
                            btnTime_txt13 = strYear + "-" + tomorrowStringDate + " " + btnTime_txt13;
                            intent.putExtra("btnTime_txt13", btnTime_txt13);
                            this.setResult(BTNTIME_TXT13, intent);
                            break;
                        case 14:
                            String btnTime_txt14 = btn14.getText().toString();
                            btnTime_txt14 = strYear + "-" + tomorrowStringDate + " " + btnTime_txt14;
                            intent.putExtra("btnTime_txt14", btnTime_txt14);
                            this.setResult(BTNTIME_TXT14, intent);
                            break;
                        case 15:
                            String btnTime_txt15 = btn15.getText().toString();
                            btnTime_txt15 = strYear + "-" + tomorrowStringDate + " " + btnTime_txt15;
                            intent.putExtra("btnTime_txt15", btnTime_txt15);
                            this.setResult(BTNTIME_TXT15, intent);
                            break;
                        case 16:
                            String btnTime_txt16 = btn16.getText().toString();
                            btnTime_txt16 = strYear + "-" + tomorrowStringDate + " " + btnTime_txt16;
                            intent.putExtra("btnTime_txt16", btnTime_txt16);
                            this.setResult(BTNTIME_TXT16, intent);
                            break;
                    }
                    finish();
                }
                break;
            case R.id.btnOk3:
                tvDate = other_date.getText().toString();
                tvTime = other_time.getText().toString();
                if (TextUtils.isEmpty(tvDate) || TextUtils.isEmpty(tvTime)) {
                    timeDialog.show();
                } else {
                    char[] chars = tvDate.toCharArray();
                    String month = chars[0] + "" + chars[1];
                    String day = chars[3] + "" + chars[4];
                    String date = month + "-" + day;//08-04
                    intent.putExtra("otherDate", strYear + "-" + date + " " + tvTime);
                    this.setResult(BTNTIME_TXT17, intent);
                    finish();
                }
                break;
        }
    }

    private void showOther() {
        evaluate_child_labes_line_03.setBackgroundColor(getResources().getColor(R.color.home_boder_color));
        evaluate_child_labes_03.setTextColor(getResources().getColor(R.color.home_boder_color));
        evaluate_child_labes_01.setTextColor(getResources().getColor(R.color.black));
        evaluate_child_labes_line_01.setBackgroundColor(getResources().getColor(R.color.border_color03));
        evaluate_child_labes_02.setTextColor(getResources().getColor(R.color.black));
        evaluate_child_labes_line_02.setBackgroundColor(getResources().getColor(R.color.border_color03));
        time1.setVisibility(View.GONE);
        time2.setVisibility(View.GONE);
        other.setVisibility(View.VISIBLE);
    }

    private void showTomorrow() {
        evaluate_child_labes_line_02.setBackgroundColor(getResources().getColor(R.color.home_boder_color));
        evaluate_child_labes_02.setTextColor(getResources().getColor(R.color.home_boder_color));
        evaluate_child_labes_01.setTextColor(getResources().getColor(R.color.black));
        evaluate_child_labes_line_01.setBackgroundColor(getResources().getColor(R.color.border_color03));
        evaluate_child_labes_03.setTextColor(getResources().getColor(R.color.black));
        evaluate_child_labes_line_03.setBackgroundColor(getResources().getColor(R.color.border_color03));
        time1.setVisibility(View.GONE);
        time2.setVisibility(View.VISIBLE);
        other.setVisibility(View.GONE);
    }

    private void showToday() {
        evaluate_child_labes_01.setTextColor(getResources().getColor(R.color.home_boder_color));
        evaluate_child_labes_line_01.setBackgroundColor(getResources().getColor(R.color.home_boder_color));
        evaluate_child_labes_02.setTextColor(getResources().getColor(R.color.black));
        evaluate_child_labes_line_02.setBackgroundColor(getResources().getColor(R.color.border_color03));
        evaluate_child_labes_03.setTextColor(getResources().getColor(R.color.black));
        evaluate_child_labes_line_03.setBackgroundColor(getResources().getColor(R.color.border_color03));
        time1.setVisibility(View.VISIBLE);
        time2.setVisibility(View.GONE);
        other.setVisibility(View.GONE);
    }

    //根据当前时间设置按钮的显示与隐藏
    private void btnHideAndVisibile() {
        //获取系统当前时间
        int hour = MyTimeUtils.getSystemHour24();
        if (hour > 16 && hour < 24) {
            hideBtn();//所有按钮都不可点击
            return;
        } else if ((hour >= 0 && hour < 9)) {
            showBtn();//所有的都可点击
            setOrangeBackground();
        } else if (hour >= 9 && hour < 10) {//btn1不可使用
            btn1.setEnabled(false);
            btn1.setTextColor(getResources().getColor(R.color.gray_mini));
            btn1.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));
            return;
        } else if (hour >= 10 && hour < 11) {//btn1不可使用 btn2不可使用
            btn1.setEnabled(false);
            btn1.setTextColor(getResources().getColor(R.color.gray_mini));
            btn1.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn2.setEnabled(false);
            btn2.setTextColor(getResources().getColor(R.color.gray_mini));
            btn2.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));
            return;
        } else if (hour >= 11 && hour < 12) {
            btn1.setEnabled(false);
            btn1.setTextColor(getResources().getColor(R.color.gray_mini));
            btn1.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn2.setEnabled(false);
            btn2.setTextColor(getResources().getColor(R.color.gray_mini));
            btn2.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn3.setEnabled(false);
            btn3.setTextColor(getResources().getColor(R.color.gray_mini));
            btn3.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));
            return;
        } else if (hour >= 12 && hour < 13) {
            btn1.setEnabled(false);
            btn1.setTextColor(getResources().getColor(R.color.gray_mini));
            btn1.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn2.setEnabled(false);
            btn2.setTextColor(getResources().getColor(R.color.gray_mini));
            btn2.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn3.setEnabled(false);
            btn3.setTextColor(getResources().getColor(R.color.gray_mini));
            btn3.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn4.setEnabled(false);
            btn4.setTextColor(getResources().getColor(R.color.gray_mini));
            btn4.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            return;
        } else if (hour >= 13 && hour < 14) {

            btn1.setEnabled(false);
            btn1.setTextColor(getResources().getColor(R.color.gray_mini));
            btn1.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn2.setEnabled(false);
            btn2.setTextColor(getResources().getColor(R.color.gray_mini));
            btn2.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn3.setEnabled(false);
            btn3.setTextColor(getResources().getColor(R.color.gray_mini));
            btn3.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn4.setEnabled(false);
            btn4.setTextColor(getResources().getColor(R.color.gray_mini));
            btn4.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn5.setEnabled(false);
            btn5.setTextColor(getResources().getColor(R.color.gray_mini));
            btn5.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            return;
        } else if (hour >= 14 && hour < 15) {
            btn1.setEnabled(false);
            btn1.setTextColor(getResources().getColor(R.color.gray_mini));
            btn1.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn2.setEnabled(false);
            btn2.setTextColor(getResources().getColor(R.color.gray_mini));
            btn2.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn3.setEnabled(false);
            btn3.setTextColor(getResources().getColor(R.color.gray_mini));
            btn3.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn4.setEnabled(false);
            btn4.setTextColor(getResources().getColor(R.color.gray_mini));
            btn4.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn5.setEnabled(false);
            btn5.setTextColor(getResources().getColor(R.color.gray_mini));
            btn5.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));
            btn6.setEnabled(false);
            btn6.setTextColor(getResources().getColor(R.color.gray_mini));
            btn6.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));
            return;
        } else if (hour >= 15 && hour < 16) {
            btn1.setEnabled(false);
            btn1.setTextColor(getResources().getColor(R.color.gray_mini));
            btn1.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn2.setEnabled(false);
            btn2.setTextColor(getResources().getColor(R.color.gray_mini));
            btn2.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn3.setEnabled(false);
            btn3.setTextColor(getResources().getColor(R.color.gray_mini));
            btn3.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn4.setEnabled(false);
            btn4.setTextColor(getResources().getColor(R.color.gray_mini));
            btn4.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn5.setEnabled(false);
            btn5.setTextColor(getResources().getColor(R.color.gray_mini));
            btn5.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));
            btn6.setEnabled(false);
            btn6.setTextColor(getResources().getColor(R.color.gray_mini));
            btn6.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

            btn7.setEnabled(false);
            btn7.setTextColor(getResources().getColor(R.color.gray_mini));
            btn7.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));
            return;
        }
    }

    //所有时间按钮都可点击
    private void showBtn() {
        btn1.setEnabled(true);
        btn1.setTextColor(getResources().getColor(R.color.black));
        btn1.setBackground(getResources().getDrawable(R.drawable.btn_selector));

        btn2.setEnabled(true);
        btn2.setTextColor(getResources().getColor(R.color.black));
        btn2.setBackground(getResources().getDrawable(R.drawable.btn_selector));

        btn3.setEnabled(true);
        btn3.setTextColor(getResources().getColor(R.color.black));
        btn3.setBackground(getResources().getDrawable(R.drawable.btn_selector));

        btn4.setEnabled(true);
        btn4.setBackground(getResources().getDrawable(R.drawable.btn_selector));
        btn4.setTextColor(getResources().getColor(R.color.black));

        btn5.setEnabled(true);
        btn5.setTextColor(getResources().getColor(R.color.black));
        btn5.setBackground(getResources().getDrawable(R.drawable.btn_selector));

        btn6.setEnabled(true);
        btn6.setTextColor(getResources().getColor(R.color.black));
        btn6.setBackground(getResources().getDrawable(R.drawable.btn_selector));

        btn7.setEnabled(true);
        btn7.setTextColor(getResources().getColor(R.color.black));
        btn7.setBackground(getResources().getDrawable(R.drawable.btn_selector));

        btn8.setEnabled(true);
        btn8.setTextColor(getResources().getColor(R.color.black));
        btn8.setBackground(getResources().getDrawable(R.drawable.btn_selector));
    }

    //所有时间按钮不可点击
    private void hideBtn() {
        btn1.setEnabled(false);
        btn1.setTextColor(getResources().getColor(R.color.gray_mini));
        btn1.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

        btn2.setEnabled(false);
        btn2.setTextColor(getResources().getColor(R.color.gray_mini));
        btn2.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

        btn3.setEnabled(false);
        btn3.setTextColor(getResources().getColor(R.color.gray_mini));
        btn3.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

        btn4.setEnabled(false);
        btn4.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));
        btn4.setTextColor(getResources().getColor(R.color.gray_mini));

        btn5.setEnabled(false);
        btn5.setTextColor(getResources().getColor(R.color.gray_mini));
        btn5.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

        btn6.setEnabled(false);
        btn6.setTextColor(getResources().getColor(R.color.gray_mini));
        btn6.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

        btn7.setEnabled(false);
        btn7.setTextColor(getResources().getColor(R.color.gray_mini));
        btn7.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

        btn8.setEnabled(false);
        btn8.setTextColor(getResources().getColor(R.color.gray_mini));
        btn8.setBackground(getResources().getDrawable(R.drawable.btn_gray_mini));

    }
    //设置选择其他时间界面上的日期和时间
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (data != null) {
            switch (resultCode) {
                case 0://设置Other界面上的时间
                    strTime = data.getStringExtra("btn_text1");
                    other_time.setText(strTime);
                    break;
                case 1:
                    strTime = data.getStringExtra("btn_text2");
                    other_time.setText(strTime);
                    break;
                case 2:
                    strTime = data.getStringExtra("btn_text3");
                    other_time.setText(strTime);
                    break;
                case 3:
                    strTime = data.getStringExtra("btn_text4");
                    other_time.setText(strTime);
                    break;
                case 4:
                    strTime = data.getStringExtra("btn_text5");
                    other_time.setText(strTime);
                    break;
                case 5:
                    strTime = data.getStringExtra("btn_text6");
                    other_time.setText(strTime);
                    break;
                case 6:
                    strTime = data.getStringExtra("btn_text7");
                    other_time.setText(strTime);
                    break;
                case 7:
                    strTime = data.getStringExtra("btn_text8");
                    other_time.setText(strTime);
                    break;
                case 8://传回来的日期
                    strData = data.getStringExtra("date");
                    other_date.setText(strData);
                    break;
            }
        }
    }

    //监听手机返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timeDialog.dismiss();
    }
}
